package com.planet.apiimpl.action;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.planet.api.domain.MessageParam;
import com.planet.apiimpl.domain.SendTaskModel;
import com.planet.common.enums.RespStatusEnum;
import com.planet.common.vo.BasicResultVO;
import com.planet.support.pipeline.BusinessProcess;
import com.planet.support.pipeline.ProcessContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * 前置的检查
 *  1.判断他是否传入模块id
 *  如果情况都有就报中断返回
 *
 *  完事去到拼装参数这边逻辑
 */

/**
 * @author yls
 * @date 2021/11/22
 * @description 参数校验
 */
@Slf4j
public class PreParamCheckAction implements BusinessProcess {

    private final Long BATCH_RECEIVER_SIZE = 100L;

    @Override
    public void process(ProcessContext context) {
        SendTaskModel sendTaskModel = (SendTaskModel) context.getProcessModel();

        Long messageTemplateId = sendTaskModel.getMessageTemplateId();
        List<MessageParam> messageParamList = sendTaskModel.getMessageParamList();
        // 没有传入 消息模板Id 或者 messageParam
        if (messageTemplateId == null || CollUtil.isEmpty(messageParamList)) {
            context.setNeedBreak(true).setResponse(BasicResultVO.fail(RespStatusEnum.CLIENT_BAD_PARAMETERS));
            return;
        }
        // 过滤 receiver=null 的messageParam
        List<MessageParam> resultMessageParamList = messageParamList.stream()
                .filter(messageParam -> !StrUtil.isBlank(messageParam.getReceiver()))
                .collect(Collectors.toList());
        if (CollUtil.isEmpty(resultMessageParamList)) {
            context.setNeedBreak(true).setResponse(BasicResultVO.fail(RespStatusEnum.CLIENT_BAD_PARAMETERS));
            return;
        }
        sendTaskModel.setMessageParamList(resultMessageParamList);
    }
}
